package stream;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class GroupBy {

	public static void main(String args[]) {

		List<User> users = Arrays.asList(new User[] { new User(123, "fan", SexEnum.M), new User(12, "zhi", SexEnum.M),
				new User(60, "zhi", SexEnum.FM) });

		// 单属性group by
		Map<String, List<User>> res = users.stream().collect(Collectors.groupingBy(User::getName));
		// 多属性group by
		Map<Object, List<User>> res2 = users.stream()
				.collect(Collectors.groupingBy(u -> Arrays.asList(u.getName(), u.getAge())));
		// 求条数
		Map<User, Long> res3 = users.stream()
				.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

		System.out.println("groupBy:" + res);
		System.out.println("countBy:" + res2);
		System.out.println("countBy:" + res3);

		
		
//		List<Foo> result = new ArrayList<>(
//			    myList.stream()
//			          .collect(Collectors.groupingBy(
//			              f -> Arrays.<String>asList(f.getType(), f.getCode()),
//			              MoreCollectors.pairing(
//			                 Collectors.collectingAndThen(MoreCollectors.first(), Optional::get),
//			                 Collectors.summingInt(Foo::getQuantity),
//			                 (f, s) -> new Foo(f.getType(), s, f.getCode())
//			              )
//			          )).values());

	 
	 
	}
}
